/*
ID:luongho3
LANG:C++
TASK:gift1
*/
#include <iostream>
#include <fstream>
using namespace std;
#define MAXNP 20
ifstream fin("gift1.in");
ofstream fout("gift1.out");
struct person {
       string name;
       int amount;
       int nToGive;
       int give;
       int receive;
};
int nPerson;
person People[MAXNP];
int findPersonPosition(string personName) {
    int i;
    for (i=1;i<=nPerson;++i) {
        if (People[i].name==personName) {
           return i;                                
        }
    }   
    return -1;
}
main() {
       fin>>nPerson;
       int i;
       for (i=1;i<=nPerson;++i) {
           fin>>People[i].name;
           People[i].amount=0;
           People[i].give=0;
           People[i].receive=0;
       }
       for (i=1;i<=nPerson;++i) {
           string name;
           fin>>name;
           int giverPosition = findPersonPosition(name);
           int t;
           fin>>t>>People[giverPosition].nToGive;
           if (People[giverPosition].nToGive!=0) {
              People[giverPosition].amount+=(t%People[giverPosition].nToGive);
              People[giverPosition].give+=(t-(t%People[giverPosition].nToGive));
              t=(t/People[giverPosition].nToGive);              
           }
           else {
                People[giverPosition].amount+=t;
           }
           int j;
           for (j=1;j<=People[giverPosition].nToGive;++j) {
               fin>>name;
               int receiverPosition = findPersonPosition(name);
               People[receiverPosition].amount+=t;
               People[receiverPosition].receive+=t;
           }
       }
       for (i=1;i<=nPerson;++i) {
           fout<<People[i].name<<" "<<(People[i].receive-People[i].give)<<"\n";    
       }
       return 0;      
}
